package com.sgb.knowledgeshare.view.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityShareCommentMagessBinding;
import com.sgb.knowledgeshare.entity.VideoReceiveListEntity;
import com.sgb.knowledgeshare.view.adapter.MagsCommentAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareCommentMagessModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import java.util.List;

/**
 * Created by：冯齐 on 2020/11/12 18:37
 * Description： 分享-消息-评论
 */
public class ShareCommentMagessActivity extends AppActivity {

    ShareCommentMagessModel viewModel;
    private ActivityShareCommentMagessBinding binding;
    private int page;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private MagsCommentAdapter magscommentadapter;
    private boolean isLoadMore;
    private List<VideoReceiveListEntity.RecordsBean> records;
    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ShareCommentMagessActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_comment_magess);
        viewModel = new ViewModelProvider(this).get(ShareCommentMagessModel.class);
        viewModel.setActivity(ShareCommentMagessActivity.this);
        binding.include.titleView.setText("评论");
        binding.include.ivBack.setOnClickListener(this);
        magscommentadapter = new MagsCommentAdapter(this, null);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(magscommentadapter);
        binding.lrecyclerview.setLayoutManager(new LinearLayoutManager(this));
        binding.lrecyclerview.setAdapter(mLRecyclerViewAdapter);
        binding.lrecyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                page = 1;
                viewModel.getVideoReceiveLists(page);
            }
        });
        binding.lrecyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                viewModel.getVideoReceiveLists(page);
            }
        });
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (TextUtils.isEmpty(records.get(position).getTitle())){
                    MToast.showToast(getApplicationContext(),"此作品已删除");
                    return;
                }
                String videoIds = records.get(position).getVideoId();
                ShareLandActivity.start(getApplicationContext(),videoIds,"comment",records.get(position));
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        isLoadMore = false;
        page = 1;
        viewModel.getVideoReceiveLists(page);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        }
    }

    //收到的评论列表
    public void setVideoReceiveLists(VideoReceiveListEntity data) {
        if (data.getRecords() == null) return;
        binding.lrecyclerview.refreshComplete(page);
        binding.lrecyclerview.setPullRefreshEnabled(true);
        if (isLoadMore) {
            if (data.getRecords().size() > 0) {
                magscommentadapter.addDatas(data.getRecords());
            } else {
                MToast.showToast(getApplicationContext(), "没有数据啦！");
            }
        } else {
            if (data.getRecords().size() > 0) {
                binding.lrecyclerview.setVisibility(View.VISIBLE);
                binding.empty.setVisibility(View.GONE);
                magscommentadapter.setDatas(data.getRecords());
            } else {
                binding.lrecyclerview.setVisibility(View.GONE);
                binding.empty.setVisibility(View.VISIBLE);
                magscommentadapter.setDatas(null);
            }
        }
        records = magscommentadapter.getDatas();
    }


}
